﻿using System;
using System.Linq;
using FluentAssertions;
using LINQPad.OpenAccess.Infrastructure;
using LINQPad.OpenAccess.Metadata.Reflection;
using LINQPad.OpenAccess.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace LINQPad.OpenAccess.Tests
{
    [TestClass]
    public class OpenAccessStaticDataContextDriverFixture
    {
        [TestMethod]
        public void TestAutoInitializationViaDefaultConstructor( )
        {
            ServiceLocator.Current.Should( ).NotBeNull( "the default constructor depends on it" );

            var driver = new OpenAccessStaticDataContextDriver( );

            // this test is a bit messy right now due to ninject issues
            // - single test => it passes cleanly
            // - all tests => it fails with an argumentnullexception, since Ninject doesn't resolve the dependencies in this case.
            // looks pretty much like a multithreading/threadsafety issue to me, 
            // but for now, it hopefully doesn't affect us while linqpad loads the driver.
            driver.IsInitialized
                  .Should( )
                  .BeTrue( "the default constructor should use the service locator to automatically inject the required dependencies and initialize the driver" );
        }
        [TestMethod]
        public void TheDriverCanOnlyBeInitializedOnce( )
        {
            var dataContextServices = new Mock<IDataContextServices>( ).Object;

            var driver = new OpenAccessStaticDataContextDriver( dataContextServices );


            driver.IsInitialized.Should( ).BeTrue( );
            driver.Services.Should( ).NotBeNull( );

            driver.Invoking
                        (
                            d => d.InitializeDataContextDriver( dataContextServices )
                        )
                  .ShouldThrow<InvalidOperationException>( );
        }

    }
}
